package com.yixing.tech.system.service.impl;

import com.yixing.tech.common.base.BaseMongoDao;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.Page;
import com.yixing.tech.common.base.PageRow;
import com.yixing.tech.common.enums.ComparisonSymbol;
import com.yixing.tech.common.enums.ErrCode;
import com.yixing.tech.common.enums.YesOrNo;
import com.yixing.tech.system.po.Role;
import com.yixing.tech.system.service.IApplicationService;
import com.yixing.tech.system.service.IMenuService;
import com.yixing.tech.system.service.IRoleService;
import com.yixing.tech.common.utils.DataUtil;
import com.yixing.tech.common.vo.s3.ApplicationVO;
import com.yixing.tech.common.vo.s3.MenuVO;
import com.yixing.tech.common.vo.s3.RoleVO;
import com.yixing.tech.common.vo.s3.UserVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yixing tech
 * at 2021/12/31
 * describe：
 */
@Service
public class RoleServiceImpl implements IRoleService {

    @Resource
    BaseMongoDao baseMongoDao;
    @Resource
    IMenuService menuService;
    @Resource
    IApplicationService applicationService;

    @Value("${official-manager-id}")
    private String officialManagerId;

    @Override
    public void add(RoleVO roleVO) throws BusinessException {
        RoleVO condition = new RoleVO();
        condition.setName(roleVO.getName());
        condition.setTenantId(roleVO.getTenantId());
        if (baseMongoDao.exists(condition, Role.class)) {
            throw new BusinessException(ErrCode.ROLE_EXISTED);
        }
        Role role = DataUtil.o2o(roleVO, Role.class);
        role.setId(null);
        baseMongoDao.save(role);
    }

    @Override
    public void batchAdd(List<RoleVO> list) throws BusinessException {

    }

    @Override
    public RoleVO queryById(String id) {
        Role role = baseMongoDao.get(id, Role.class);
        return DataUtil.o2o(role, RoleVO.class);
    }

    @Override
    public RoleVO queryById(String id, UserVO loginUser) {
        Role role = baseMongoDao.get(id, Role.class);
        RoleVO vo = DataUtil.o2o(role, RoleVO.class);
        List<ApplicationVO> parentApps = applicationService.list(new ApplicationVO(), loginUser.getId());
        vo.setApplications(parentApps);
        return vo;
    }

    @Override
    public List<MenuVO> queryMenusByApp(String id, ApplicationVO app, UserVO loginUser) throws BusinessException {
        MenuVO cond = new MenuVO();
        cond.setApplication(app);
        cond.setTenantId(loginUser.getTenantId());
        List<MenuVO> parentMenuList = menuService.list(cond, loginUser);
        return parentMenuList;
    }

    @Override
    public List<RoleVO> queryByIds(List<String> ids) {
        return null;
    }

    @Override
    public RoleVO queryByCondition(RoleVO roleVO) throws BusinessException {
        return null;
    }

    @Override
    public void update(RoleVO roleVO) throws BusinessException {

        String name = roleVO.getName();
        if (StringUtils.hasText(name)) {
            RoleVO condition = new RoleVO();
            condition.setName(roleVO.getName());
            condition.setId(roleVO.getId());
            condition.setTenantId(roleVO.getTenantId());
            condition.putComparisonSymbols("id", ComparisonSymbol.ne);
            if (baseMongoDao.exists(condition, Role.class)) {
                throw new BusinessException(ErrCode.ROLE_EXISTED);
            }
        }
        List<String> menuIds = roleVO.getMenuIds();
        menuIds = menuService.fillParentId(menuIds);
        roleVO.setMenuIds(menuIds);
        Role role = DataUtil.o2o(roleVO, Role.class);
        baseMongoDao.update(role);
    }

    @Override
    public Page<RoleVO> list(PageRow<RoleVO> pageRow) {
        Criteria criteria = baseMongoDao.condition2Criteria(pageRow.getCondition(RoleVO.class));
        Page<Role> page = baseMongoDao.listPage(PageRequest.of(pageRow.getPageNo(), pageRow.getPageSize()), Role.class, criteria);
        List<Role> list = page.getData();
        List<RoleVO> listVO = list.stream().map(m -> DataUtil.o2o(m, RoleVO.class)).collect(Collectors.toList());
        return new Page<>(listVO, page.getPageSize(), page.getPageNo(), page.getTotalCount());
    }

    @Override
    public List<RoleVO> list(RoleVO condition) {
        Criteria criteria = baseMongoDao.condition2Criteria(condition);
        List<Role> list = baseMongoDao.listAll(Role.class, criteria);
        List<RoleVO> listVO = list.stream().map(m -> DataUtil.o2o(m, RoleVO.class)).collect(Collectors.toList());
        return listVO;
    }

    @Override
    public void deleteById(String id) throws BusinessException {
        Role role = new Role();
        role.setId(id);
        role.setIsDelete(YesOrNo.YES.ordinal());
        baseMongoDao.update(role);
    }

    @Override
    public void delete(List<String> ids) throws BusinessException {

    }
}
